/*
 * Copyright (c) 2022 Kirky
 * Licensed under the Open Software Licence, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/osl-3.0.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lingeringdream.luciole.slice;

import com.lingeringdream.luciole.ResourceTable;
import com.lingeringdream.luciole.helper.list.NormalListProvider;
import com.lingeringdream.luciole.model.entity.Privates;
import com.lingeringdream.luciole.model.entity.User;
import com.lingeringdream.luciole.model.helper.GetEntityHelper;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.List;

/**
 * 隐私清单页面
 *
 * @author Kirky
 * @version 1.0
 * @date 2022/5/4
 */
public class PrivateSlice extends AbilitySlice {
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0x11, PrivateSlice.class.getSimpleName());
    private NormalListProvider provider;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_slice_normal_list);

        initTitleBar();
        initList();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (provider != null) {
            provider.clear();
            provider = null;
        }
    }

    /**
     * 初始化titleBar
     */
    private void initTitleBar() {
        Button backButton = findComponentById(ResourceTable.Id_titleBar_back);
        backButton.setClickedListener(component -> terminate());

        Text title = findComponentById(ResourceTable.Id_titleBar_title);
        title.setText("隐私清单");
    }

    /**
     * 初始化隐私清单列表
     */
    private void initList() {
        provider = new NormalListProvider(getContext()).setLine(true).setArrow(true).setOnlyContent(true);
        List<Privates> privates = GetEntityHelper.getPrivateUser(getContext(), null);
        if (privates == null) {
            HiLog.info(TAG, "privates is null.");
        } else {
            for (Privates private_ : privates) {
                provider.addItemOnlyContent(private_.getName(), component -> {
//                   跳转至隐私详情
                    Intent intent = new Intent();
                    intent.setParam("name", private_.getName());
//                    present(, intent);
                });
            }
        }
    }

    /**通过服务器获取隐私清单*/
    private void getPrivates() {

    }
}
